/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_queue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>

#include "jh_queue.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
//数据类型为int，按从小到大的方式排序：
int compare(const void* a, const void* b) {
     return ( *(int*)a - *(int*)b );
}

int main(int argc, char* argv[])
{
    //定义数据结构和用于存放数据的数组
    jh_queue_t queue;
    int buf[10];
    
    //队列初始化
    jh_queue_init(&queue, buf, 10, sizeof(int));
    
    //定义要用于传入的数据
    int items[] = {81, 13, 16, 38, 49, 67};
    int i, val;
    
    //数据入队
    printf("push :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_queue_push(&queue, &items[i]))
            printf("%d ", items[i]);
    }
    printf("\n");
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_queue_count(&queue), jh_queue_is_empty(&queue), jh_queue_is_full(&queue));
    
    //查看队首的数据
    jh_queue_peek(&queue, &val);
    printf("peek %d\n", val);
    
    //查看队尾的数据
    jh_queue_peek_tail(&queue, &val);
    printf("peek tail %d\n", val);
    
    //数据出队
    printf("\npop:\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_queue_pop(&queue, &val))
            printf("%d ", val);
    }
    printf("\n");
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_queue_count(&queue), jh_queue_is_empty(&queue), jh_queue_is_full(&queue));
    
    
    //数据入队
    printf("\npush :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_queue_push(&queue, &items[i]))
            printf("%d ", items[i]);
    }
    printf("\n");
    
    //可以查看当前所有数据，从队首到队尾的顺序遍历
    printf("\niter:\n");
    JH_QUEUE_ITER(&queue, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //可以查看当前所有数据，从队尾到队首的顺序遍历
    printf("\niter reverse:\n");
    JH_QUEUE_ITER_REVERSE(&queue, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //将队尾数据出队，可以作为栈使用
    printf("\npop tail:\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_queue_pop_tail(&queue, &val))
            printf("%d ", val);
    }
    printf("\n");
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_queue_count(&queue), jh_queue_is_empty(&queue), jh_queue_is_full(&queue));
    
    
    //数据入队
    printf("\npush :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_queue_push(&queue, &items[i]))
            printf("%d ", items[i]);
    }
    printf("\n");
    
    //清空队列
    printf("\nclear\n");
    jh_queue_clear(&queue);
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_queue_count(&queue), jh_queue_is_empty(&queue), jh_queue_is_full(&queue));
    
    
    
    int sortitems[] = {81, 16, 38, 13, 109, 67, 49, 87, -88};
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_queue_count(&queue), jh_queue_is_empty(&queue), jh_queue_is_full(&queue));
    
    //数据入队
    printf("\npush tail:\n");
    for(i = 0; i < sizeof(sortitems)/sizeof(int); i++) {
        if(!jh_queue_push(&queue, &sortitems[i]))
            printf("%d ", sortitems[i]);
    }
    printf("\n");
    
    //可以查看当前所有数据，从队首到队尾的顺序遍历
    printf("\niter:\n");
    JH_QUEUE_ITER(&queue, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //排序
    jh_queue_sort(&queue, compare);
    
    //打印排序后的数据
    printf("sort\n");
    JH_QUEUE_ITER(&queue, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    return 0;
}